/************************************************************************************/
/*  Program name CAPFNDIMP_RESP_FUNCTIONS.sql                                       */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFNDIMP_RESP_FUNCTIONS';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FND_RESP_FUNCTIONS;

	lv_Next_Seq_ID	NUMBER;
	lv_Action_ID	NUMBER;

BEGIN

	FOR C1 IN Cur_F LOOP
		BEGIN
			IF	C1.RULE_TYPE	=	'M'	THEN
				lv_Action_ID	:=	CAPBRIMP_MAPPING.GetFNDMenuIDMapping(C1.Action_ID);
			ELSE
				lv_Action_ID	:=	C1.ACTION_ID;
			END IF;

			INSERT INTO FND_RESP_FUNCTIONS
			VALUES
			(
			C1.APPLICATION_ID,
			CAPBRIMP_MAPPING.GetFNDRespIDMapping(C1.RESPONSIBILITY_ID),		--RESPONSIBILITY_ID,
			lv_Action_ID,
			C1.RULE_TYPE,
			C1.LAST_UPDATE_DATE,										--LAST_UPDATE_DATE,
			C1.LAST_UPDATED_BY,			
			C1.LAST_UPDATE_LOGIN,
			C1.CREATION_DATE,
			C1.CREATED_BY						--CREATED_BY,
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
